<?php

// table 'faq'
class Faq extends Model
{
    var $id = null;
    var $question;
    var $answer;
	var $position;
    var $active = 1;
	var $online = 1;
	var $created_at;
	var $updated_at;

    function Faq()
    {
        parent::Model();
    }

    function get_by_id($id)
	{
		$this->db->select('f.id, f.question, f.answer, f.online, f.created_at, f.updated_at, f.position, f.creator_id, f.updater_id, creator.name as creator_name, creator.firstname as creator_firstname, updater.name as updater_name, updater.firstname as updater_firstname')
				 ->from('faq f')
				 ->join('user creator', 'creator.id = f.creator_id')
				 ->join('user updater', 'updater.id = f.updater_id')
				 ->where('f.id', $id)
				 ->where('f.active', 1);

		$query = $this->db->get();
		return $query->row();
	}

	function plain_get($id)
	{
		$this->db->select('id, question, answer, online, created_at, updated_at, position')
				 ->from('faq')
				 ->where('id', $id)
				 ->where('active', 1);

		$query = $this->db->get();
		return $query->row();
	}

	function get_next_position()
	{
		$this->db->select('max(position) as last_position')
		         ->from('faq')
		         ->where('active', 1);
		$result = $this->db->get()->result();
		return intval($result[0]->last_position) + 1;
	}

   	/**
	 * Liste de tous les questions
	 */
	function all()
	{
		$this->db->select('id, question, answer, online, created_at, updated_at, position')
		         ->from('faq')
		         ->where('active', 1)
				 ->order_by('position');
		$query = $this->db->get();
		return $query->result();
	}

	/**
	 * Tous les questions publiées
	 */
	function onlines()
	{
		$this->db->select('id, question, answer, created_at, updated_at')
		         ->from('faq')
				 ->where('active', 1)
				 ->where('online', 1)
				 ->order_by('position');
		$query = $this->db->get();
		return $query->result();
	}

	/**
	 * Sauvegarder l'enregistrement
	 */
	function save($faq)
	{
		if ($faq->id == null) {
			return $this->db->insert('faq', $faq);
		}
		else {
			$this->db->where('id', $faq->id);
			return $this->db->update('faq', $faq);
		}
	}

	/**
	 * Supprimer une question
	 */
	function delete($id)
	{
		$sql = 'UPDATE faq
		        SET active = 0
				WHERE id = '.intval($id).';';
		return $this->db->query($sql);
	}

	function restore($id)
	{
		$sql = 'UPDATE faq
		        SET active = 1
				WHERE id = '.intval($id).';';
		return $this->db->query($sql);
	}

	/**
	 * Vérifier et affecter les données utlisateurs
	 * @return liste des erreurs
	 */
	function set_data(&$faq, &$data)
	{
		$errors = array();
		if (empty($data['question'])) {
			$errors[] = 'La question ne peut pas être vide';
		}
		else {
			$faq->question = clean_str($data['question']);
		}
		if (empty($data['answer'])) {
			$errors[] = 'La réponse ne peut pas être vide';
		}
		else {
			$faq->answer = clean_str_preserve_tags($data['answer']);
		}
		$faq->online = isset($data['online']);
		return $errors;
	}
}
